<template>
  <div class="container" style="height: 100vh;width: 300px;padding: 20px;box-sizing: border-box;width: 100%;">
    <header>
      <MBreadcrumb :item="breadcrumbItem" />
    </header>
    <main>
      <MTitle class="title">
        <template #left>
          采购申请
        </template>
      </MTitle>
      <form>
        <table id="attributeTable" class="mtable">
          <tr v-for="(obj, index) in MListItem" :key="index" class="mtr">
            <td class="field">{{ obj.field }}</td>
            <td class="value" v-if="String(obj.field) != '验收标准'&&String(obj.field) != '交货周期'">
              <MInput v-model="ProductInfo[index]" style="font-size: 12px;width: 90%" :disabled="obj.disabled"></MInput>
            </td>
            <td class="value" v-if="String(obj.field) == '交货周期'">
              <MSelect v-model="ProductInfo[index]" :options="[{value:'一周',text:'一周'},{value:'二周',text:'二周'},{value:'三周',text:'三周'},{value:'四周',text:'四周'}]" :value="ProductInfo[index]" id="ruletype"
                    required style="flex: 1 1 200px;width: 90%;">
                  </MSelect>
            </td>
            <td class="value" v-if="String(obj.field) == '验收标准'">
              <textarea v-model="ProductInfo[index]" required
                style="width: 90%;border:1px solid #d2d2d2;border-radius: 5px;resize: none;padding: 5px;box-sizing: border-box;font-size: 12px;"
                rows="5">
        </textarea>
            </td>
          </tr>
        </table>
      </form>

    </main>
    <footer>
      <div style="text-align: center;" v-if="!isDisable">
        <MButton @click="applyOrder">申请</MButton>
      </div>
    </footer>
    <MMessage v-if="toastVisible" :message="toastMessage" :state="toastState" />

  </div>
</template>

<script>
import { ref, onMounted } from 'vue';
import MBreadcrumb from "../../components/MBreadcrumb.vue"
import MTitle from "../../components/MTitle.vue"
//import MList from "../../components/MList.vue"
import MButton from "../../components/MButton.vue"
import MInput from "../../components/MInput.vue"
import MMessage from '../../components/MMessage.vue';
import MSelect from '../../components/MSelect.vue';

//import MTag from "../../components/MTag.vue"
import axios from "../../axios/axios"
export default {
  name: "PurchaseApply",
  components: {
    MBreadcrumb,
    MTitle,
    //MList,
    MButton,
    MInput,
    MMessage,
    MSelect
    //MTag
    //MIcon
  },
  setup() {
    const toastVisible = ref(false)
    const toastMessage = ref("")
    const toastState = ref("success")
    const breadcrumbItem = ref(
      {
        key: "0",
        title: '赋码管理',
        children: {
          key: "0-0",
          title: '采购申请',
        }
      })
    //const userId="12121"
    const MListItem = ref([])
    const ProductInfo = ref([])
    const tagItems = ref([])
    const params = ref([])
    const userId = ref("")
    const isDisable = ref(false)
    const applyOrder = () => {
      if (chectValue()) {
        isDisable.value = true
        // console.log({
        //   userid: params.value.get('userId'),
        //   trainingid: params.value.get("trainingId"),
        //   applydepartment: ProductInfo.value[0],
        //   applicant: ProductInfo.value[1],
        //   applyphone: ProductInfo.value[2],
        //   applyemail: ProductInfo.value[3],
        //   goods: ProductInfo.value[4],
        //   goodsmodel: ProductInfo.value[5],
        //   goodsnumber: ProductInfo.value[6],
        //   goodsprice: ProductInfo.value[7],
        //   generalbudget: ProductInfo.value[8],
        //   purchasepurpose: ProductInfo.value[9],
        //   allocation: ProductInfo.value[10],
        //   deliverytime: ProductInfo.value[11],
        //   installdemand: ProductInfo.value[12],
        //   checkstandard: ProductInfo.value[13],
        // });

        axios.post('/apply/save', {
          userid: params.value.get('userId'),
          trainingid: params.value.get("trainingId"),
          applydepartment: ProductInfo.value[0],
          applicant: ProductInfo.value[1],
          applyphone: ProductInfo.value[2],
          applyemail: ProductInfo.value[3],
          goods: ProductInfo.value[4],
          goodsmodel: ProductInfo.value[5],
          goodsnumber: ProductInfo.value[6],
          goodsprice: ProductInfo.value[7],
          generalbudget: ProductInfo.value[8],
          purchasepurpose: ProductInfo.value[9],
          allocation: ProductInfo.value[10],
          deliverytime: ProductInfo.value[11],
          installdemand: ProductInfo.value[12],
          checkstandard: ProductInfo.value[13],
        })
          .then(response => {
            if (response.data.Code == "200") {
              messageShow("已申请", "success")

            } else {
              messageShow(response.data.Msg, "warning")
              isDisable.value = false
            }
          })
      }

    }
    const items = [
      {
        "title": "申请部门",
        "value": "采购部"
      },
      {
        "title": "申请人",
        "value": "张三"
      },
      {
        "title": "联系电话",
        "value": "12345678911"
      },
      {
        "title": "电子邮箱",
        "value": "123456@qq.com"
      },
      {
        "title": "物品信息",
        "value": "电视机 ",
        "disabled": true
      },
      {
        "title": "规格型号",
        "value": "75寸4k超高清智能电视",
        "disabled": true
      },
      {
        "title": "数量",
        "value": "100",
        "disabled": true
      },
      {
        "title": "预计单价",
        "value": "2550",
        "disabled": true
      },
      {
        "title": "总预算",
        "value": "255000",
        "disabled": true
      },
      {
        "title": "采购目的",
        "value": "客房电视机更新"
      },
      {
        "title": "分配计划",
        "value": "客房分配100台"
      },
      {
        "title": "交货周期",
        "value": "二周"
      },
      {
        "title": "安装要求",
        "value": "我方自行安装"
      },
      {
        "title": "验收标准",
        "value": "符合国家相关规定"
      }
    ]
    const chectValue = () => {
      const hasEmpty = ProductInfo.value.some(function (item, index) {
        if (item === "") {
          messageShow("请输入" + MListItem.value[index].field, "warning");
          return true;
        }
        return false;
      });

      return !hasEmpty;
    }
    const messageShow = (msg, state) => {
      toastMessage.value = msg;
      toastVisible.value = true;
      toastState.value = state
      // 使用 setTimeout 模拟 Toast 组件内部的定时器功能（仅用于演示）
      // 在实际使用中，你应该依赖 Toast 组件内部的定时器
      setTimeout(() => {
        toastVisible.value = false;
      }, 3000); // 与 Toast 组件中的 duration 属性保持一致
    }
    onMounted(() => {
      // 获取 URL 参数
      const param = new URLSearchParams(window.location.search);
      params.value = param
      if (param.has('userId')) {
        userId.value = param.get('userId');
      }
      if (param.has('isDisable')) {
        isDisable.value = param.get('isDisable') === "true"
      }
      const info = items
      var dataList = []
      var dataList1 = []
      info.forEach((element, index) => {
        dataList.push({
          key: index,
          field: element.title,
          value: element.value,
          disabled: element.disabled
        },)
        dataList1.push(element.value)
      });
      MListItem.value = dataList
      ProductInfo.value = dataList1
    });
    return {
      toastVisible,
      toastMessage,
      toastState,
      breadcrumbItem,
      MListItem,
      tagItems,
      ProductInfo,
      isDisable,
      applyOrder
    };
  },
};
</script>

<style scoped>
.title {
  margin-top: 30px;
}

.title1 {
  margin-top: 20px;
}

.mtr {
  display: flex;
  flex-wrap: wrap;
  /* 允许子元素换行 */
  align-items: flex-start;
}

.container {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

footer {
  text-align: center;
  height: 10%;
}

main {
  flex: 1;
}

.mtable {
  width: 100%;
  font-size: 13px;

}

.field {
  text-align: left;
  padding: 4px 0;
  flex: 1 1 60px;
  word-break: break-word;
  /* 允许长单词换行 */
  overflow-wrap: break-word;
  min-width: 0;
  /* 确保内容可以缩小 */
  height: 26px;
  line-height: 26px;
}

.value {
  text-align: right;
  padding: 4px 0;
  flex: 1 1 150px;
  word-break: break-word;
  /* 允许长单词换行 */
  overflow-wrap: break-word;
  min-width: 0;
  height: 26px;
  line-height: 26px;
  /* 确保内容可以缩小 */
}
</style>
